﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Newtonsoft.Json;
using Meeting.Model;
using Components;

namespace Meeting.Web.ashx
{
    public partial class SaveTopicApprove : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string header = "{0}";
            //bool jsonP = false;
            try
            {
                //String cb = Request.Params["callback"]; //  
                ////传过来的 callback 参数 假如 Ext.data.JsonP.callback  
                //if (cb != null)
                //{
                //    jsonP = true;
                //    Response.ContentType = "text/javascript";
                //}
                //else
                //{
                //    Response.ContentType = "application/x-json;charset=utf-8";
                //}

                //header = "";
                //if (jsonP)
                //{
                //    header = cb + "({0})";
                //}
                //else
                //{
                //    header = "{0}";
                //}

                var jSetting = new JsonSerializerSettings();
                jSetting.NullValueHandling = NullValueHandling.Ignore;
                Model.TopicApprove approve = JsonConvert.DeserializeObject<Model.TopicApprove>(Request["param"], jSetting);

                if (approve != null)
                {
                    //Response.ContentType = "application/x-json;charset=utf-8";

                    String savePath = Server.MapPath("~/upload/approve/topic");

                    if (string.IsNullOrEmpty(approve.Path))
                    {
                        //按年月生成文件夹
                        approve.Path = StrHelp.MakeFolderName();
                    }
                    string folder = savePath + "/" + approve.Path + "/" + approve.TopicId.ToString();

                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }

                    byte[] imageData = Convert.FromBase64String(approve.Comment);
                    FileStream fs = File.Create(folder + "/" + approve.EmployeeId + ".png");
                    fs.Write(imageData, 0, imageData.Length);
                    fs.Close();

                    //保存成功之后在数据库中插入一条附注记录
                    BLL.TopicBLL bll = new BLL.TopicBLL();
                    approve.Comment = "";
                    Response.Write(string.Format(header, bll.SubmitApprove(approve)));
                    //Response.Flush();
                    Response.End();
                }
            }
            catch (Exception ex)
            {
                JsonResponse response = new JsonResponse();
                response.success = false;
                response.Message = ex.Message;
                Response.Write(string.Format(header, JsonConvert.SerializeObject(response)));
            }
        }
    }
}